/**
* 他人详情页
* path:'/otherdetail' ,query:{userCode:'他人用户ID，必传'}
*/
<template>
  <div class="situation other-details">
    <header-top :option="headerOption"></header-top>
    <overview :clientId="clientId" :overviewOption="overviewOption" :medalsOwned="medalsOwned"></overview>
    <div class="wave">
      <img src="~assets/images/bowen@3x.png" width="100%" height="40px">
    </div>
    <chart :userCode="userCode" :clientId="clientId" :groupId="''" :entrustType="'1'" :userType="'other'"></chart>
    <div class="other-position" :class="!isLogin?'logout':''" ref="otherPosition">
      <div class="other-position-list">
        <position-transfer :clientId="clientId"></position-transfer>
        <position-stock :clientId="clientId"></position-stock>
        <position-group :clientId="clientId"></position-group>
        <position-fund :clientId="clientId"></position-fund>
      </div>
      <div class="not-login-bg" v-show="!isLogin" ref="notLoginBg">
        <div><img src="~assets/images/portfolio/haixiuniu.png"></div>
        <div><span>你还不是持仓用户</span><span>持仓羞羞了</span></div>
        <div class="button" @click.stop.prevent="register">立即注册</div>
      </div>
    </div>
    <games-fighting :clientId="clientId"></games-fighting>
    <record :clientId="clientId"></record>
    <similar-man :clientId="clientId"></similar-man>
    <div class="blank-60h"></div>
    <foot-btn :clientId="clientId" :userCode="userCode" :goodPointCount="goodPointCount"></foot-btn>
    <subscribe-cancel-modal></subscribe-cancel-modal>
    <subscribe-modal></subscribe-modal>
  </div>
</template>

<script>
  import RemoteDAO from 'common/remoteDAO'
  import CommonUtil from 'common/common.util'
  import LocalDAO from 'common/localDAO'
  import Constant from 'common/constant'
  import HeaderTop from 'components/HeaderTop'
  import OverView from 'components/dynamic/otherdetail/OverView'
  import Chart from 'components/Chart'
  import PositionTransfer from 'components/dynamic/otherdetail/PositionTransfer'
  import PositionStock from 'components/dynamic/otherdetail/PositionStock'
  import PositionGroup from 'components/dynamic/otherdetail/PositionGroup'
  import PositionFund from 'components/dynamic/otherdetail/PositionFund'
  import GamesFighting from 'components/dynamic/otherdetail/GamesFighting'
  import Record from 'components/dynamic/otherdetail/Record'
  import SimilarMan from 'components/dynamic/otherdetail/SimilarMan'
  import FootBtn from 'components/dynamic/otherdetail/FootBtn'
  import SubscribeCancelModal from 'components/modal/SubscribeCancelModal'
  import SubscribeModal from 'components/modal/SubscribeModal'

  export default {
    data () {
      return {
        userCode: LocalDAO.getUserCode(),//用户自己的ID
        clientId: '', //牛人的ID
        headerOption: {
          title: '牛人牛股',
          leftBtn: Constant.leftBtn.BACK,
          operations: [Constant.operation.TUTORIAL, Constant.operation.REFRESH, Constant.operation.SHARE],
          page:Constant.storage.SHARE_OTHER_DETAILS
        },
        overviewOption: {
          userHead: '',
          nickname: '',
          subscribeUserCount: '--',
          features: '', //用户标签
        },
        medalsOwned: {list: []}, //已获勋章
        subscribePrice: '0',//订阅价格
        goodPointCount: '0', //点赞数,
        isLogin:false,
        error: ''
      }
    },
    components: {
      'header-top': HeaderTop,
      'overview': OverView,
      'chart': Chart,
      'position-transfer': PositionTransfer,
      'position-stock': PositionStock,
      'position-group': PositionGroup,
      'position-fund':PositionFund,
      'games-fighting': GamesFighting,
      'record': Record,
      'similar-man': SimilarMan,
      'foot-btn': FootBtn,
      'subscribe-cancel-modal': SubscribeCancelModal,
      'subscribe-modal': SubscribeModal,
    },
    created: function () {
      this.fetchData();
      // 更新分享页数据
      var sharePage = LocalDAO.getShareOtherDetails();
      sharePage.adDownload = {userCode: this.userCode};
      LocalDAO.setShareOtherDetails(sharePage);
    },
    // fetch移到mounted里面 
    mounted:function(){
      this.getCss()
    },
    watch: {
      '$route.query.userCode' (to, from) {
        //刷新参数放到这里里面去触发就可以刷新相同界面了
        this.fetchData()
      }
    },
    methods: {
      fetchData () {
        var self = this;
        self.clientId = self.$route.query.userCode;
        if(LocalDAO.getNickname()){
          self.isLogin = true;
        }else{
          self.isLogin = false
        }
        // 查牛人个人信息
        RemoteDAO.callUC12000104(self.clientId, function (response) {
          if (Number(response.error) === 0) {
            var data = response.data.body[0];
            var userHead = data.USER_HEAD || CommonUtil.getDefaultUserHead(self.clientId);
            var nickname = data.NICKNAME;
            var subscribeUserCount = data.SUBSCRIBE_USER_COUNT; // 被订阅数
            var walletTotal = CommonUtil.toFixed(data.WALLET_TOTAL, 0); //勋章数
            var goodpointCount = data.GOODPOINT_COUNT; //点赞数
            var subscribePrice = CommonUtil.toFixed(data.SUBSCRIBE_PRICE, 0); //被订阅价格
            if (subscribePrice == "") {
              subscribePrice = "0";
            }
            self.goodPointCount = goodpointCount;
            self.subscribePrice = subscribePrice;

            // 获取用户标签
            var userTagList = [];
            var features = [];
            try {
              userTagList = JSON.parse(response.data.body[0].USER_TAG_LIST);
            } catch (error) {
              console.error("解析用户标签错误", error.message);
            }
            _.each(userTagList, function (item, index) {
              if (index <= 2) {
                features.push(item.TAG_NAME);
              }
            });
            features = features.join(" ") == "" ? "" : features.join(" ")

            // 获取用户勋章
            var medalsOwned = {list:[]};
            var list = [];
            try {
              var honorList = JSON.parse(response.data.body[0].HONOR_LIST);
              _.each(honorList, function (item, index) {
                var object = {};
                object.medalCode = item.HONOR_CODE;
                object.medalName = item.HONOR_NAME;
                object.iconURL = "";
                list.push(object);
              });
              medalsOwned.list = list;
            } catch (err) {
              self.$store.dispatch('setTip', {text: '解析已获取勋章列表返回值失败'});
            }

            self.overviewOption = {
              userHead: userHead,
              nickname: nickname,
              subscribeUserCount: subscribeUserCount,
              features: features,
            };
            LocalDAO.setOtherNickname(nickname);
            LocalDAO.setOtherUserHead(userHead);
            LocalDAO.setOtherWalletTotal(walletTotal);
            LocalDAO.setOtherMedalsOwned(medalsOwned);

            //查询所有勋章,获取各勋章详细资料
            RemoteDAO.callUC12000042(self.clientId, "", function (response) {
              if (Number(response.error) === 0) {
                _.each(response.data.body, function (item, index) {
                  var object = {};
                  object.iconURL = item.ICON_URL;  //勋章图地址
                  object.honorName = item.HONOR_NAME; //勋章名称
                  object.honorCode = item.HONOR_CODE; //勋章代码
                  object.configType = item.CONFIG_TYPE; //勋章类型
                  object.walletNum = item.CONFIG_VALUE.WALLET_NUM; //奖励君元宝数
                  object.taskDesc = item.CONFIG_VALUE.TASK_DESC; //任务说明
                  // 为 medalsOwned 赋值勋章图片路径和获得勋章奖励君元宝数
                  _.each(list, function (item, index) {
                    if (item.medalCode == object.honorCode) {
                      item.iconURL = object.iconURL;
                      item.walletNum = object.walletNum;
                      return false
                    }
                  });
                });

                //最后得到字段完全的已获勋章列表
                self.medalsOwned.list = list;
                LocalDAO.setOtherMedalsOwned(self.medalsOwned);

                // 更新分享页数据
                var shareOtherDetails = LocalDAO.getShareOtherDetails();
                shareOtherDetails.overview.userHead = userHead;
                shareOtherDetails.overview.nickname = nickname;
                shareOtherDetails.overview.subscribeUserCount = subscribeUserCount;
                shareOtherDetails.overview.subscribePrice = subscribePrice;
                shareOtherDetails.overview.features = features;
                shareOtherDetails.medalsOwned = self.medalsOwned;
                LocalDAO.setShareOtherDetails(shareOtherDetails);
              } else {
                self.$store.dispatch('setTip', {text: "UC12000042" + response.data});
              }
            });

          } else {
            self.$store.dispatch('setTip', {text: "UC12000104" + response.data});
          }
        });
      },
      getCss(){
        if(!this.isLogin){
          var w = Number(window.screen.width);//屏幕的宽度
          //令未注册提醒背景图高度与屏幕分辨率相适应
          var h = w*588/750;
          this.$refs.otherPosition.style.height = h-30+"px";
          this.$refs.notLoginBg.style.height = h+"px";
        }
      },
      register(){
        var self = this;
        self.$store.dispatch('setLoading',{visible:true});
        RemoteDAO.callNativeLogin(function (response) {
          if(response.error == 0){
            window.location.reload();
          }else{
            self.$store.dispatch('setTip',{text:response.error});
          }
        });
        var kv = {"usercode":"","k2":""};
        TDAPP.onEvent("2003","",kv);
        TDAPP.send();
      }
    }
  }
</script>
